#!/bin/bash

init_osc() {
  :
}

cleanup_osc() {
  :
}


get_instance_by_fip() {
    local fip=$(_parse $1 ip)
    shift
    openstack server list --column Networks --column ID -f value $* |grep -w $fip |awk '{print $1}'
}

# XXX: multi network ?
# net-for-use=192.168.2.28
# net-for-use=192.168.3.13, 172.16.113.2
get_floatingIP() {
    openstack server list --column Networks -f value $* |awk '{print $2}'
}

reboot_instance() {
    local vm=$1
    shift
    echo "openstack server reboot --hard $vm"
    openstack server reboot --hard $vm $*
}

rebuild_instance() {
    local vm=$1
    shift
    echo "openstack server rebuild $vm"
    openstack server rebuild $vm $*
}

show_instance() {
    local vm=$1
    shift
    openstack server show $vm $*
    openstack console url show $vm $*
}

get_instance_status() {
    local vm=$1
    shift
    local status=$(openstack server show $vm $* -c status -f value)
    echo $status
}

get_instance_image() {
    local vm=$1
    shift
    openstack server show $vm -c image -f value $* \
    |sed 's/.*\([0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}\).*/\1/'
}

get_network_id(){
    local portid=$1
    shift
    openstack port show $portid -c network_id -f value $*
}

get_image_os_properties() {
    local image=$1
    shift
    openstack image show $image -c properties -f value $*
}

get_image_os_distro() {
    local image=$1
    shift
    echo -e "try:\n\
 d=$(openstack image show $image -c properties -f value $*)\n\
 print d['os_distro']\n\
except:\n\
 pass\n\
" |python -
}

get_port_device_owner() {
  local portid=$1
  shift
  openstack port show $portid -c device_owner -f value $*
}

get_port_id() {
  local project=$1
  local fip=$2
  local s=
  shift 2
  [ -n "$project" ] && s="--project $project"
  openstack port list -f value $s $* |grep -w $fip |awk '{print $1}'
}

